import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController, ActionSheetController} from 'ionic-angular';

// 调用相机插件
import { Camera, CameraOptions} from '@ionic-native/camera';
import { ImagePicker, ImagePickerOptions} from "@ionic-native/image-picker";

@IonicPage()
@Component({
  selector: 'page-camera',
  templateUrl: 'camera.html',
})
export class CameraPage {

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public camera: Camera,
              public imgPicker: ImagePicker,
              public toastCtrl: ToastController,
              public actionSheetCtrl: ActionSheetController) {
  }

    public img={
        "isShow": false,
        "title":'请点击按钮操作',
        "url":'',
        "camPath":'',
        "imgPath":[]
    }

    // quality: 100,
    // sourceType: this.camera.PictureSourceType.CAMERA,
    // destinationType: this.camera.DestinationType.DATA_URL,
    // encodingType: this.camera.EncodingType.JPEG,
    // mediaType: this.camera.MediaType.PICTURE

    startCamera(){
        console.log('开始使用相机');
        const options: CameraOptions = {
            quality: 50,
            destinationType: 1, // Camera.DestinationType.FILE_URI,
            sourceType: 1, // Camera.PictureSourceType.CAMERA,
            encodingType: 0, // Camera.EncodingType.JPEG,
            mediaType: 0 // Camera.MediaType.PICTURE,
        }


        this.camera.getPicture(options).then((imageData) => {
            // 获取成功
            let base64Image = 'data:image/jpeg;base64,' + imageData;
            this.img.isShow=true;
            this.img.title="拍照成功！";
            this.img.camPath = base64Image;
        }, (err) => {
            this.img.isShow=false;
            this.img.title="拍照失败！";
            this.showToast('ERROR:' + JSON.stringify(err));
        });
    }

    openImgPicker(){
        console.log('开始调用相册');
        const options: ImagePickerOptions = {
            maximumImagesCount: 1,//选择一张图片
            width: 800,
            height: 800,
            quality: 80
        }

        var imgPaths = [];
        this.imgPicker.getPictures(options)
            .then((results) => {
                for (var i = 0; i < results.length; i++) {
                    var img = {
                        "id": i+1,
                        "url": results[i]
                    }

                    imgPaths.push(img);
                }
                this.img.imgPath = imgPaths;

                this.img.isShow=true;
                this.img.title="选择照片成功！";
            }, (err) => {
                this.img.isShow=false;
                this.img.title="选择照片失败！";
                this.showToast('ERROR:' + JSON.stringify(err)); //错误：无法从手机相册中选择图片！
            });

    }

    openSheet() {
        let actionSheet = this.actionSheetCtrl.create({
            title: '选择',
            buttons: [
                {
                    text: '拍照',
                    handler: () => {
                        this.startCamera();
                    }
                },
                {
                    text: '从手机相册选择',
                    handler: () => {
                        this.openImgPicker();
                    }
                },
                {
                    text: '取消',
                    role: 'cancel',
                    handler: () => {

                    }
                }
            ]
        });
        actionSheet.present();
    }
    static TOAST_POS_BOTTOM: string = 'bottom';
    static TOAST_POS_MIDDLE: string = 'middle';

    // 提示
    showToast(message: string, position: string = CameraPage.TOAST_POS_BOTTOM) {
        let toast = this.toastCtrl.create({
            message: message,
            duration: 1500,
            position: position
        });
        toast.present();

        return toast;
    }

  ionViewDidLoad() {
    console.log('ionViewDidLoad CameraPage');
  }

}
